var tableclass = {
    rowCreator : function(options) {
        var row = document.createElement('tr');
        if (options.rowNum % 2 == 0) {
            row.className = "even";
        } else {
            row.className = "odd";
        }
        var classnametemp;
        row.onmouseover = function() {
            classnametemp = row.className;
            // alert(classnametemp);
            row.className = "over";
        };
        row.onmouseout = function() {
            row.className = classnametemp;
        };
        return row;
    },
    cellCreator : function(options) {
        var cell = document.createElement('td');
        cell.id = "autotd" + options.cellNum;
        if (options.cellNum == 0) {
            cell.style.cssText = "color:#000000;white-space:nowrap;text-align:right;";
        } else {
            cell.style.cssText = "color:#000000;white-space:nowrap;text-align:center;";
        }
        return cell;
    }
};
function send() {
    var paramsize = arguments.length;
    var flag = true;
    var params;
    if (paramsize == 1) {
        params = arguments[0];
    } else if (paramsize == 2) {
        params = arguments[1];
        params.page = arguments[0];
    } else if (paramsize == 3) {
        params = arguments[1];
        params.page = arguments[0];
        params.exefunc = arguments[2];
    } else {
        flag = false;
        alert("\u53c2\u6570\u9519\u8bef\uff01");
    }
	
    if (flag) {
        initParams(params);
        // alert(params.funcname+'(params.page, params.pagesize, showdata)')
        //count not null
        if (params.count||params.count==0) {
        	//count is full function name and params, not number
            if (isNaN(params.count)) {
            	//select total record function
                params.countfunc = params.count;
                params.countfunc = params.countfunc.replace(')', 'showcount)');
            }
            if (params.countfunc) {
                try {
                    eval(params.countfunc);
                } catch (e) {
                    eval(params.funcname.substring(0, params.funcname
                        .indexOf('.'))
                    + '.' + params.countfunc);
                }
            } else {
                if (params.count >= 0) {
                    initPageNavigation(params);
                } else {
                	//total record error
                    alert('\u9519\u8bef\u7684\u8bb0\u5f55\u603b\u6570(count:' + params.count + ')');
                }
            }
			
        } else {
        	//count is null
            eval(params.funcname.substring(0, params.funcname.indexOf('.'))
                + '.getCount(showcount)');
        }
        // pagecount not null
        if (params.pagecount) {
            if (params.page <= params.pagecount) {
                eval(params.funcname
                    .replace(
                        ')',
                        'params.page, params.pagesize, {callback:showdata,errorHandler:function(message) { alert("\u672a\u77e5\u9519\u8bef!"); }})'));

            } else {
                params.page = params.prvpage;
                initPageNavigation(params);
                alert('\u8f93\u5165\u4e86\u9519\u8bef\u7684\u9875\u6570(\u9875\u6570\u8303\u56f41-' + params.pagecount + ')');
            }
        } else {
            eval(params.funcname.replace( ')','params.page, params.pagesize, {callback:showdata,errorHandler:function(message) { alert("\u6ca1\u6709\u7b26\u5408\u6761\u4ef6\u7684\u6570\u636e!"); }})'));
        }
		
    }
    
    function showcount(data) {
        params.count = data;
        initPageNavigation(params);
    }
    
    function showdata(data) {
        params.prvpage = params.page;
        DWRUtil.removeAllRows(params.idname + '_data');
        if(data&&data.length>0){
        	DWRUtil.addRows(params.idname + '_data', data, params.cellfuncs,params.tableclass);
       	 	if (params.exefunc) {
            	params.exefunc();
        	}
        }else{
        	var tr = $(params.idname + '_data').appendChild(document.createElement("tr"));
        	var td = tr.appendChild(document.createElement("td"));
        	td.colSpan=params.cellfuncs.length;
        	td.style.textAlign="center";
        	td.style.color="#ff0000";
        	td.appendChild(document.createTextNode("\u6ca1\u6709\u6570\u636e!"));
        }
    }
}

function NoNumZ(num) {
    if (num !== null && !isNaN(num) && num > 0
        && Math.ceil(num) == Math.floor(num)) {
        return false;
    } else {
        return true;
    }
}

function initParams(params) {
    if (!params.prvpage) {
        params.prvpage = 1;
    }
    if (!params.tableclass) {
        params.tableclass = tableclass;
    }
    if (!params.funcname) {
        flag = false;
        alert("\u53c2\u6570funcname\u4e3a\u5fc5\u6dfb\u9879\uff01");
    }
    if (!params.cellfuncs) {
        flag = false;
        alert("\u53c2\u6570cellfuncs\u4e3a\u5fc5\u6dfb\u9879\uff01");
    }
    if (!params.paramname) {
        flag = false;
        alert("\u53c2\u6570paramname\u4e3a\u5fc5\u6dfb\u9879\uff01");
    }
    if (!params.buttonname) {
        params.buttonname = 'GO';
    }
    if (!params.spiltsignone) {
        params.spiltsignone = '';
    }
    if (!params.spiltsigntwo) {
        params.spiltsigntwo = '';
    }
    if (!params.firstpage) {
        params.firstpage = '\u9996\u9875';
    }
    if (!params.lastpage) {
        params.lastpage = '\u672b\u9875';
    }
    if (!params.pageup) {
        params.pageup = '\u4e0a\u4e00\u9875';
    }
    if (!params.pagedown) {
        params.pagedown = '\u4e0b\u4e00\u9875';
    }
    if (!params.gotopage) {
        params.gotopage = '\u8df3\u8f6c\u5230';
    }
    if (!params.idname) {
        params.idname = 'sorc' + Math.round(Math.random() * 10000);
    }
    if (NoNumZ(params.page)) {
        params.page = 1;
    }
    if (NoNumZ(params.pagesize)) {
        params.pagesize = 10;
    }
}

function initPageNavigation(param) {
    var params = param;
    //if (NoNumZ(params.pagecount))
    params.pagecount = Math.ceil(params.count / params.pagesize) == 0
    ? 1
    : Math.ceil(params.count / params.pagesize);

    if (NoNumZ(params.navigationsize)) {
        params.navigationsize = params.pagecount;
    }
    if(params.page>params.pagecount){
       
        params.page=params.pagecount;
        send(params);
    }
    if(params.count&&params.count==-5){
        alert('\u767b\u9646\u8d85\u65f6,\u8bf7\u91cd\u65b0\u767b\u9646\uff01');
        location=URLParams['basePath']?URLParams['basePath']:'/';
    }
    var navigation = 1;
   
    navigation = Math.ceil(params.page / params.navigationsize);
    var navigationCount = 1;
    
    navigationCount = Math.ceil(params.pagecount / params.navigationsize);
    var beginpage = 1;
    // beginpage = (navigation - 1) * params.navigationsize + 1;
    barZone = $(params.idname + '_bar');
    var tsb = "<span class='pageNavBar'>";
    tsb += (params.page + '/' + params.pagecount + '\u9875'
        + params.count + '\u6761\u8bb0\u5f55');
    tsb += "</span>";
    // 首页
    tsb += "<span class='pageNavBar'>";
    if (params.page == 1) {
        tsb += (params.spiltsignone + " " + params.firstpage
            + "" + params.spiltsigntwo );
    } else {
        tsb += ("<a href=\"#\"");
        tsb += (" onclick=\"" + params.paramname + ".page=1;");
        tsb += ("send(" + params.paramname + ")\">" + params.spiltsignone + " "
            + params.firstpage + "" + params.spiltsigntwo + "</a>");

    }
    tsb += "</span>\n";
    tsb += "<span class='pageNavBar'>";
    // if (navigationCount > 1) {
    if (params.page <= 1) {
        tsb += (params.spiltsignone + "" + params.pageup + ""
            + params.spiltsigntwo);
    } else {
        tsb += ("<span class='pageNavBar'><a href=\"#\"");
        tsb += (" onclick=\"" + params.paramname + ".page=" + (params.page - 1) + ";");
        tsb += ("send(" + params.paramname + ")\">" + params.spiltsignone + ""
            + params.pageup + "" + params.spiltsigntwo + "</a>");
    }
    tsb += "</span>\n";
    // }

    // 中间页
    var beginpagetemp = 1;
    beginpagetemp = params.page - Math.round((params.navigationsize) / 2) + 1;
    if (beginpagetemp > 0) {
        beginpage = beginpagetemp;
    } else {
        beginpage = 1;
    }
	
    for (var i = beginpage; i <= Math.min(
        beginpage + params.navigationsize - 1, params.pagecount); i++) {
        tsb += ("<span class='pageNavBar'>");
        if (i == params.page) {
            tsb += ("" + params.spiltsignone + "" + i + ""
                + params.spiltsigntwo + "");
        } else {
            tsb += ("<a href=\"#\")\" onclick=\"" + params.paramname + ".page="
                + i + ";send(" + params.paramname + ")\">"
                + params.spiltsignone + "" + i + "" + params.spiltsigntwo + "</a>");
        }
        tsb += ("</span>\n");
    }
    
    // if (navigationCount > 1) {
    tsb += ("<span class='pageNavBar'>");
    if (params.page >= params.pagecount) {
        tsb += (params.spiltsignone + "" + params.pagedown + ""
            + params.spiltsigntwo);
    } else {
        tsb += ("<a href=\"#\" style=\"text-decoration:none;\" onclick=\""
            + "send("
            + (params.page + 1)
            + ","
            + params.paramname
            + ")\">"
            + params.spiltsignone
            + ""
            + params.pagedown
            + ""
            + params.spiltsigntwo);
    }
    tsb += ("</span>\n");
    // }
    tsb += ("<span class='pageNavBar'>");
    if (params.page == params.pagecount) {
        tsb += (params.spiltsignone + "" + params.lastpage + ""
            + params.spiltsigntwo + "\n");
    } else {
        tsb += ("<a href=\"#\" style=\"text-decoration:none;\" onclick=\""
            + "send("
            + params.pagecount
            + ","
            + params.paramname
            + ")\">"
            + params.spiltsignone
            + ""
            + params.lastpage
            + ""
            + params.spiltsigntwo + "</a>");
    }
    tsb += ("</span>\n");
    tsb += ("<span class='pageNavBar'>");
    tsb += ( params.gotopage
        + "<input class=\"inputgoto\" maxlength=\""
        + (params.pagecount.toString().length)
        + "\" style=\"width:"
        + (params.pagecount.toString().length * 7 + 10)
        + "\" type=\"text\" id=\""
        + params.idname
        + "_goto\" name=\""
        + params.idname
        + "_goto\" value=\""
        + params.page
        + "\">\u9875<input type=\"button\" class=\"inputgo\" name=\"inputgo\" value=\""
        + params.buttonname + "\" onclick=\"" + params.paramname
        + ".page=DWRUtil.getValue('" + params.idname + "_goto');send("
        + params.paramname + ");\">");
    tsb += ("</span>\n");
    barZone.innerHTML = tsb;
}